﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ShopBLL;
using ShopModel;
using System.Threading;
using System.Globalization;
using System.IO;

public partial class _default : System.Web.UI.MasterPage
{
    #region VARIABLES

    public ContentTemplate AboutS;
    public ContentTemplate ContactS;
    public ContentTemplate DeliveryS;
    public ContentTemplate InstalementsS;
    public ContentTemplate RulesS;
    public ContentTemplate ShipmentS;
    public ContentTemplate OfferS;
    private string _ProductMetaTag = string.Empty;

    #endregion VARIABLES

    #region PROPERTIES

    public int OpenCategoryId
    {
        get
        {
            return leftMenu.OpenCategoryId;
        }

        set
        {
            leftMenu.OpenCategoryId = value;
            //   searchMenu.CategoryId = value;
        }
    }

    public string ProductMetaTag
    {
        get { return _ProductMetaTag; }
        set { _ProductMetaTag = value; }
    }

    #endregion PROPERTIES

    #region METHODS

    private void LoadResource(string lang)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
    }

    private void GenerateMetaTags()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<meta name=\"description\" content=\"");
        sb.Append(ReportsManager.Instance.GetContentTemplate("MetaTagDescription", CommonTools.CurrentLanguageID).Content);
        sb.Append("\"/>");
        sb.Append("<meta name=\"keywords\" content=\"");
        sb.Append(ReportsManager.Instance.GetContentTemplate("MetaTagKeywords", CommonTools.CurrentLanguageID).Content);

        if (ProductMetaTag != string.Empty)
            sb.Append("," + ProductMetaTag);

        sb.Append("\"/>");
        lMetaTags.Text = sb.ToString();
    }

    private void dlLanguagesDataBind()
    {
        dlLanguages.DataSource = ReportsManager.Instance.GetAvailableLanguages(true);
        dlLanguages.DataBind();
    }

    /* private void BindThemes()
     {
         DirectoryInfo dInfo = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("App_Themes"));
         DirectoryInfo[] dArrInfo = dInfo.GetDirectories();
         List<Theme> list = new List<Theme>();
         foreach (DirectoryInfo sDirectory in dArrInfo)
         {
             Theme temp = new Theme(sDirectory.Name);
             list.Add(temp);
         }

         ddlThemes.DataSource = list;
         ddlThemes.DataBind();
         ddlThemes.SelectedValue = Page.Theme;
     }*/

    #endregion METHODS

    #region EVENTS

    protected void LanguageChangeCommand(object sender, CommandEventArgs e)
    {
        Language NewLanguage = ReportsManager.Instance.GetLanguageByID(Convert.ToInt32(e.CommandArgument));

        if (NewLanguage.OutUrl != string.Empty)
            Response.Redirect(NewLanguage.OutUrl);
        else
        {
            HttpCookie cookie = new HttpCookie("lang");
            cookie.Value = NewLanguage.Culture;
            Response.SetCookie(cookie);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(NewLanguage.Culture);
            CommonTools.SetCurrentLanguageID();
            //Session["CategoryList"] = null;
            Response.Redirect(Request.RawUrl);
        }
    }

    /* protected void ddlThemes_SelectedIndexChanged(object sender, EventArgs e)
     {
         Session.Add("PageTheme", ddlThemes.SelectedValue);
         HttpCookie cookie = Request.Cookies["theme"];
         if (cookie != null)
         {
             cookie.Value = ddlThemes.SelectedValue;
             Request.Cookies.Set(cookie);
         }
         Server.Transfer(Request.FilePath);
     }*/

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Init(object sender, EventArgs e)
    {
        /*  if (!IsPostBack)
              BindThemes();*/

        if (CommonTools.GetInstallationStatus() != CommonTools.InstallationStatus.Installed)
            HttpContext.Current.Response.Redirect("~/install/default.aspx");

        dlLanguagesDataBind();
        //if (Session["CategoryList"] == null)
        //{
        CategoryList newCategoryList = new CategoryList();

        newCategoryList.LoadCategory(ProductsManager.Instance.GetCategories(true, ShopBLL.CommonTools.CurrentLanguageID));
        //            Session.Add("CategoryList", newCategoryList);
        leftMenu.DataList = newCategoryList;
        //}
        //else
        //{
        //    leftMenu.DataList = (CategoryList)Session["CategoryList"];
        //}
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GenerateMetaTags();

            dFooter.InnerHtml = ShopBLL.CommonTools.GeneratePageFooter();
            pnlTopMenu.Visible = this.Page.User.Identity.IsAuthenticated && Page.User.IsInRole("Customer");
            pnlTopMenu_CMS.Visible = this.Page.User.Identity.IsAuthenticated && (Page.User.IsInRole("ShopAssistant") || Page.User.IsInRole("ShopManager") || Page.User.IsInRole("Administrator"));

            ctrlPartners.BindControl = ShopModel.Settings.ShowPartners;
            ctrlProductsToCompare.BindControl = ShopModel.Settings.ShowProductsToCompare;
            ctrlBestSellers.BindControl = ShopModel.Settings.ShowBestsellers;
            ctrlNews.BindControl = ShopModel.Settings.ShowNews;
        }

        AboutS = ReportsManager.Instance.GetContentTemplate("About", CommonTools.CurrentLanguageID);
        ContactS = ReportsManager.Instance.GetContentTemplate("Contact", CommonTools.CurrentLanguageID);
        DeliveryS = ReportsManager.Instance.GetContentTemplate("Delivery", CommonTools.CurrentLanguageID);
        InstalementsS = ReportsManager.Instance.GetContentTemplate("Instalments", CommonTools.CurrentLanguageID);
        RulesS = ReportsManager.Instance.GetContentTemplate("Regulation", CommonTools.CurrentLanguageID);
        ShipmentS = ReportsManager.Instance.GetContentTemplate("Shipment", CommonTools.CurrentLanguageID);
        OfferS = ReportsManager.Instance.GetContentTemplate("Offer", CommonTools.CurrentLanguageID);
    }

    #endregion PAGE EVENTS
}
